#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <error.h>

#include "tcpsrv.h"

extern struct queue_table * queue_head[3];
extern struct event_table * event_head;
extern char *squidhome;
extern char *squidgroup[MAX_GROUP];

// Loop thread function
void * loop_queue_table(void * arg)
{
	int queue_interval_id = atoi((char *) arg);
	int queue_interval, i = 0, item_id, ct = 0;
	struct queue_table * curr;
	time_t now = time((time_t *) NULL);
	time_t end_time;
	unsigned char commend[512];
	char CMDTYPE[10];
	char URL[256];
	char squidclient[250] = {};
	sprintf(squidclient,"%sbin/squidclient",squidhome);

	if (queue_interval_id == 0)
	{
		queue_interval = 3600;
	}
	
	else if (queue_interval_id == 1)
	{
		queue_interval = 60;
	}
	
	else
	{
		queue_interval = 1;
	}
	
	curr = queue_head[queue_interval_id];
	while (curr)
	{
		for (i = 0; i < QUEUE_TABLE_SIZE; i ++)
		{
			end_time = curr -> table[i] . end_time;
			item_id = curr -> table[i] . item_id;
			
			if (item_id > 0 && end_time > 0 && (end_time - now < queue_interval))
			{
				// Move it
				if (queue_interval_id < 2)
				{
					queue_item_add(item_id, end_time, queue_interval_id + 1);
					curr -> table[i] . item_id = 0;
					ct ++;
					//printf("Event Move...%d from %d to %d, time : %d\n", item_id, queue_interval_id, queue_interval_id + 1, now);
				}
				
				// End it
				else
				{
					struct event_item * ei = event_item_find(item_id);
					
					if(!ei->cmd){
						event_item_delete(item_id);
					}
					else{	
						sscanf(ei->cmd,"%[A-Z]://%s",CMDTYPE,URL);
						int i=0;
						char host[4][4] = {"","","",""};
						char port[6] = {};
						char comm[512] = {};
						if (strcmp(CMDTYPE,"PURGE\0") == 0 || strcmp(CMDTYPE,"PURGEDIR\0") == 0)
						{
							if (strcmp(CMDTYPE,"PURGE\0") == 0){
								for (i=0;i<MAX_GROUP;i++){
									if (strlen(squidgroup[i])){
										//printf("host:%s",squidgroup[i]);
										sscanf(squidgroup[i],"%[0-9].%[0-9].%[0-9].%[0-9]:%s",host[0],host[1],host[2],host[3],port);
										//sprintf(comm,"%s -h %s.%s.%s.%s -p %s -U manager -W 123456 -m PURGE %s",squidclient,host[0],host[1],host[2],host[3],port,ei->cmd);
										//printf("%s",comm);
										//system(comm);
										sprintf(comm,"%s.%s.%s.%s",host[0],host[1],host[2],host[3]);
										purgeurl(URL,comm);
									}
								}
							}
							else if (strcmp(CMDTYPE,"PURGEDIR\0") == 0){
								for (i=0;i<MAX_GROUP;i++){
									if (strlen(squidgroup[i])){
										sscanf(squidgroup[i],"%[0-9].%[0-9].%[0-9].%[0-9]:%s",host[0],host[1],host[2],host[3],port);
										sprintf(comm,"%s.%s.%s.%s:%s",host[0],host[1],host[2],host[3],port);
										purgedir(comm,URL);
									}
								}
							}
						//	printf("%s\n",comm);
						}
						//sprintf(commend,"update test set `name` = 'a' where id = 1 limit 1");
					
						//printf ("%s",ei->cmd);
					}
					
					// Many times
					if (ei -> loop > 1)
					{
						//printf("Event End Already...%d, time : %d\n", item_id, now);
						curr -> table[i] . end_time += ei -> interval;
						ei -> loop --;
					}
					
					// Only 1 time left
					else if (ei -> loop == 1)
					{
						curr -> table[i] . item_id = 0;
						//printf("Event End Already...%d, time : %d\n", item_id, now);
						event_item_delete(item_id);
					}
					
					// Unlimited times
					else
					{
						//printf("Event End Already...%d, time : %d\n", item_id, now);
						curr -> table[i] . end_time += ei -> interval;
					}
					
					ct ++;
				}
			}
		}
		
		curr = curr -> next;
	}
	
	//return ct;
}
